package com.example.mscloud.controller;


import com.example.mscloud.common.entities.Payment;
import com.example.mscloud.common.entities.result.CommonResult;
import com.example.mscloud.service.FeignOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("feign/hystrix")
@DefaultProperties(defaultFallback = "fallback_global")
public class FeignOrderController {

    @Resource
    private FeignOrderService orderService;

    @GetMapping("order/payment/info/normal/{id}")
    public CommonResult<Payment> paymentInfoNormal(@PathVariable("id") Long id){
        return orderService.normalPaymentInfo(id);
    }

    @GetMapping("order/payment/info/timeout/{id}")
    @HystrixCommand(fallbackMethod = "fallback_timeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public CommonResult<Payment> paymentInfoTimeout(@PathVariable("id") Long id){
        return orderService.timeoutPaymentInfo(id);
    }

    @GetMapping("order/payment/info/exception/{id}")
    @HystrixCommand
    public CommonResult<Payment> paymentInfoException(@PathVariable("id") Long id){
        return orderService.exceptionPaymentInfo(id);
    }

    public CommonResult<Payment> fallback_timeout(Long id){
        return new CommonResult(500, "等待超时。。。", null);
    }

    public CommonResult<Payment> fallback_global(){
        return new CommonResult(500, "全局降级处理响应。。。", null);
    }
}
